<%@page import="java.util.Collections"%>
<%@page import="java.util.Arrays"%>
<%@page import="org.amj.wn.SchemaItem.UIOptions"%>
<%@page import="com.google.appengine.api.datastore.Query.FilterOperator"%>
<%@page import="org.amj.wn.UIElement"%>
<%@page import="com.google.appengine.api.datastore.FetchOptions"%>
<%@page import="com.google.appengine.api.datastore.Entity"%>
<%@page import="java.util.List"%>
<%@page import="com.google.appengine.api.datastore.Query"%>
<%@page import="org.amj.wn.Property"%>
<%@page import="org.amj.wn.WNUserService"%>
<%@page import="org.amj.wn.WaqfeNau"%>
<%@page
	import="com.google.appengine.api.datastore.DatastoreServiceFactory"%>
<%@page import="com.google.appengine.api.datastore.DatastoreService"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>New Entry</title>
<link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
</head>
<body style="width: 90%">

	<jsp:include page="admincommon.jsp"></jsp:include>

<%
	String property = request.getParameter("property");
	String operator = request.getParameter("operator");
	String value = request.getParameter("value");

	String property2 = request.getParameter("property2");
	String operator2 = request.getParameter("operator2");
	String value2 = request.getParameter("value2");
%>

	<div class="section">Search
	
	<form method="post" action="search.jsp">
		<select name="property">
			<option value=''></option>
			<%
				for(Property pr : Property.sortedByCaption()) {
					if(pr.getUiElement() == UIElement.FIELD && pr.getUiOption() != UIOptions.hidden) {
						%><option value='<%=pr%>' <%=pr.name().equals(property) ? "selected" : "" %>><%=pr.getCaption() %></option><%
					}
				}
			%>
		</select>
		<select name="operator">
			<%
				FilterOperator op = FilterOperator.EQUAL;
				%>
				<option value='<%=op.name()%>' <%=op.name().equals(operator) ? "selected" : "" %>><%=op %></option>
				<%
				op = FilterOperator.LESS_THAN;
				%>
				<option value='<%=op.name()%>' <%=op.name().equals(operator) ? "selected" : "" %>><%=op %></option>
				<%
				op = FilterOperator.GREATER_THAN;
				%>
				<option value='<%=op.name()%>' <%=op.name().equals(operator) ? "selected" : "" %>><%=op %></option>
				<%
				op = FilterOperator.LESS_THAN_OR_EQUAL;
				%>
				<option value='<%=op.name()%>' <%=op.name().equals(operator) ? "selected" : "" %>><%=op %></option>
				<%
				op = FilterOperator.LESS_THAN_OR_EQUAL;
				%>
				<option value='<%=op.name()%>' <%=op.name().equals(operator) ? "selected" : "" %>><%=op %></option>
		</select>
		<input type="text" name="value" value="<%=value == null ? "" : value%>"/>
		
		<input type="hidden" name="property2" value="<%=property2 == null ? "" : property2%>"/>
		<input type="hidden" name="operator2" value="<%=operator2 == null ? "" : operator2%>"/>
		<input type="hidden" name="value2" value="<%=value2 == null ? "" : value2%>"/>
		
		<input type="submit" value="Search" class="input_button"/>
		<a href="search.jsp">Clear Search</a>
	</div>
	<br />
	<%
  		
	  String start = request.getParameter("start");
  		int startIndex = 0;
  		try {
  			startIndex = Integer.parseInt(start);
  		} catch(Exception exp) {}
  		if(startIndex < 0)
  			startIndex = 0;
  
  		int pageSize = 20;
    List<Entity> wns = null;
    
    if(property2 == null || "".equals(property2))
    	wns = WaqfeNau.search(startIndex, pageSize, property, operator, value, null, null, null); //Uses projection. Change projection in code before using output fields
    else {
    	wns = WaqfeNau.search(startIndex, pageSize, property, operator, value, property2, operator2, value2); //Uses projection. Change projection in code before using output fields
    	%>
    		<div class="info">
    			Additional filter: <%=Property.valueOf(property2).getCaption() + " " + operator2 + " '" + value2 + "'"%>
    		</div>
    	<%
    }
    
    
    	
    	
    if (wns.isEmpty()) {
        %>
	<p>No records.</p>
	<%
    } else {
    	%>
    
    
	<div class="datagrid">
    	
	<table>
		<tr>
			<th>Member Code</th>
			<th>Waqfe Nau Number</th>
			<th>First Name</th>
			<th>Last Name</th>
			<th>Jama'at</th>
			<th>Email</th>
			<th>Home Number</th>
			<th>Cell Number</th>
		</tr>
		<%
        for (Entity wn : wns) {
                %>


		<tr>
			<td><a
				href="/admin/editentry.jsp?wn=<%=wn.getProperty(Property.memberCode.toString()) %>"><%=wn.getProperty(Property.memberCode.toString()) %></a></td>
			<td><%=wn.getProperty(Property.WaqfNumber.toString()) %></td>
			<td><%=wn.getProperty(Property.firstName.toString()) %></td>
			<td><%=wn.getProperty(Property.lastName.toString()) %></td>
			<td><%=wn.getProperty(Property.jamaat.toString()) %></td>
			<td><%=wn.getProperty(Property.email.toString()) %></td>
			<td><%=wn.getProperty(Property.homeNumber.toString()) %></td>
			<td><%=wn.getProperty(Property.cellNumber.toString()) %></td>

		</tr>

		<%
        }
        
        %>
	</table>
	</div>
	<%
    }
	%>
	<br>
	
	<input id="start" type="hidden" name="start"/>
	
	<input value="Previous" type="button" onclick=" document.getElementById('start').value=<%=startIndex - pageSize%>; this.form.submit()" />
	<% if(!wns.isEmpty()) { %>
	<input value="Next" type="button" onclick=" document.getElementById('start').value=<%=startIndex + pageSize%>; this.form.submit()" />
	<%} %>
	</form>
	</body>
</html>
